﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using LinqKit;
using System.Net.Http;
using Software41.Tracking.Model;


using System.Text;
using System.Xml;
using Software41.Tracking.Web.ViewModels;
using Software41.Tracking.Data;

namespace Software41.Tracking.Web.Controllers
{
    public partial class ProcessTraceInfoController : Controller
    {
        private readonly IProcessTraceInfoRepository processTraceInfoRepo = null;

        public ProcessTraceInfoController(IProcessTraceInfoRepository processTraceInfoRepo)
        {
            this.processTraceInfoRepo = processTraceInfoRepo;
        }

        public virtual ActionResult Dashboard()
        {
            return View();
        }

        public virtual ActionResult Search()
        {
            return View();
        }

        public virtual ActionResult SearchCriteria()
        {
            return PartialView(MVC.ProcessTraceInfo.Views.SearchCriteria);
        }

        [HttpGet]
        public virtual ActionResult SearchResults()
        {
            return PartialView(MVC.ProcessTraceInfo.Views.SearchResults, new List<ProcessTraceInfo>());
        }

        [HttpPost]
        public virtual ActionResult SearchResults(ProcessTraceInfoSearchCriteria processInfoCriteria)
        {
            var filterCriteria = PredicateBuilder.True<ProcessTraceInfo>();

            if (!string.IsNullOrWhiteSpace(processInfoCriteria.Name))
                filterCriteria = filterCriteria.And(pi => pi.Name == processInfoCriteria.Name);

            if (!string.IsNullOrWhiteSpace(processInfoCriteria.UserName))
                filterCriteria = filterCriteria.And(pi => pi.UserName == processInfoCriteria.UserName);

            if (processInfoCriteria.Begin != null)
            {
                DateTime begin = processInfoCriteria.Begin.Value.ToUniversalTime();
                filterCriteria = filterCriteria.And(pi => pi.Begin >= begin);
            }

            if (processInfoCriteria.End != null)
            {
                DateTime end = processInfoCriteria.End.Value.ToUniversalTime();
                filterCriteria = filterCriteria.And(pi => pi.End <= end);
            }

            var processInfoResults = this.processTraceInfoRepo.QueryForProcess(filter: filterCriteria, orderBy: q => q.OrderBy(p => p.Begin));

            return PartialView(MVC.ProcessTraceInfo.Views.SearchResults, processInfoResults);
        }

        public virtual ActionResult ProcessInfoDetails(string processInfoActivityId)
        {
            ProcessTraceInfo pi = this.processTraceInfoRepo.GetProcessInfo(processInfoActivityId);
            return View(pi);
        }

        public virtual ActionResult StepInfoDetails(string stepInfoActivityId)
        {
            StepInfo si = this.processTraceInfoRepo.GetStepInfo(stepInfoActivityId);
            return View(si);
        }

        public virtual PartialViewResult ActionInfoDetails(string actionInfoActivityId)
        {
            ActionInfo ai = this.processTraceInfoRepo.GetActionInfo(actionInfoActivityId);
            return PartialView(ai);
        }

        public virtual ActionResult ExceptionDetail(string exceptionDetailActivityId)
        {
            ExceptionDetail ei = this.processTraceInfoRepo.GetExceptionDetail(exceptionDetailActivityId);
            return PartialView(ei);
        }

        public virtual ActionResult ReferenceDocumentDetails(DocReferenceData docReferenceData)
        {
            DocReferenceData doc = this.processTraceInfoRepo.GetDocReferenceDataDetails(docReferenceData);
            return PartialView(doc);
        }



    }
}
